package com.sjsoft.webservice;

import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import android.content.Context;

import com.sjsoft.interfaces.OnUploadingCompleteListener;
import com.sjsoft.interfaces.OnUploadingStartListener;
import com.sjsoft.model.IRequestItem;
import com.sjsoft.model.videoItem;
/**
 * this thread run back ground and, maybe It'll get 3 task list photo, audio,
 * video
 * 
 * @author Ducv
 */
public class GetVideoThread extends Thread {

	private final Context context;
	private IRequestItem item;
	public static final int ACTION_UPLOADING = 4;
	public static final int ACTION_COMPLETE = 2;
	public static final int ACTION_CANCELLED = 3;
	

	public static final int IDLE = -1;	
	public static final int LOAD_CATEGORY_VIDEO = 0;
	public static final int LOAD_VIDEO_BY_CATEGORY = 1;
	public static final int LOAD_CATEGORY_COMPLETE = 2;

	public static int curState = IDLE;
	public static int state = IDLE;
	// Network status
	private OnUploadingCompleteListener onDownloadCompleteListener;
	private OnUploadingStartListener onDownloadStartListener;

	public GetVideoThread(Context context, IRequestItem item) {
		this.context = context;
		this.item = item;
		this.start();
	}

	@Override
	public void run() {
		try {
			onPreExecute();
			onPostExecute(doInBackground());
		} finally {
			this.interrupt();
		}
	}

	protected HttpResponse doInBackground() {
		if (item != null) {
			try {
				if (curState != IDLE) {
					ArrayList<videoItem> listVideo = request(item);
					if (GetVideoThread.state == ACTION_UPLOADING) 										
						ServiceQueueUploadingVideo.pushDownloadTaskQueue(context, item);
					else 
					{
						ServiceQueueUploadingVideo.pushDownloadTaskQueue(context, item);							
						ServiceQueueUploadingVideo.runDownloadQueue(context);
					}

					if (onDownloadCompleteListener != null)
						onDownloadCompleteListener.onGetVideoComplete(context, listVideo);
															
				}

			} catch (Exception e) {
				return null;
			}
		}
		return null;
	}

	protected void onCancelled() {
		state = ACTION_CANCELLED;
		curState = IDLE;
	}

	protected void onCancelledAll() {
		state = ACTION_CANCELLED;
		curState = IDLE;
	}

	protected void onPostExecute(HttpResponse response) {
		state = ACTION_COMPLETE;
		if (response == null) {
			if (ServiceQueueUploadingVideo.downloadQueue != null) {
				ServiceQueueUploadingVideo.runDownloadQueue(context);
			}
		} else {
			if (onDownloadCompleteListener != null) {
				try {
					String json = EntityUtils.toString(response.getEntity());
					onDownloadCompleteListener.OnGetDataComplete(context, json);
				} catch (Exception e) {
					// TODO: handle exception
				}

			}
		}
		if (ServiceQueueUploadingVideo.downloadQueue != null) {
			ServiceQueueUploadingVideo.runDownloadQueue(context);
		}
	}

	protected void onPreExecute() {
		state = ACTION_UPLOADING;

		if (onDownloadStartListener != null) {
			onDownloadStartListener.OnGetDataStart("");
		}
	}

	protected void onProgressUpdate(Integer values) {
		if (item != null) {
			if (state == ACTION_CANCELLED) {
				if (ServiceQueueUploadingVideo.downloadQueue != null) {
					ServiceQueueUploadingVideo.runDownloadQueue(context);
				}
			}
		}
	}

	private ArrayList<videoItem> request(IRequestItem item) {
		return ServiceQueueUploadingVideo.requestVideo(item.getListExtend().get(
				curState));
	}

	public OnUploadingCompleteListener getOnDownloadCompleteListener() {
		return onDownloadCompleteListener;
	}

	public void setonGetDataCompleteListener(
			OnUploadingCompleteListener onDownloadCompleteListener) {
		this.onDownloadCompleteListener = onDownloadCompleteListener;
	}

	public OnUploadingStartListener getOnDownloadStartListener() {
		return onDownloadStartListener;
	}

	public void setOnGetDataStartListener(
			OnUploadingStartListener onDownloadStartListener) {
		this.onDownloadStartListener = onDownloadStartListener;
	}
}
